<template>
  <div class="m-page">
    <el-form
      class="m-card"
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="appKey" prop="appKey">
        <el-input
          v-model="queryParams.appKey"
          placeholder="请输入appKey"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >查询</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <div class="m-card m-margin-top">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增</el-button
          >
        </el-col>
      </el-row>

      <el-table
        v-if="postList && postList.length > 0"
        v-loading="loading"
        :data="postList"
        class="m-margin-top"
        header-cell-class-name="m-table-header"
        stripe
      >
        <el-table-column label="appKey" align="center" prop="appKey" />
        <el-table-column label="密钥" align="center" prop="secret" />
        <el-table-column label="公司名称" align="center" prop="companyName" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="授权点" align="center" prop="permissionStr" show-overflow-tooltip />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleEdit(scope.row)"
            >编辑</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

    </div>
    <!-- 添加或修改授权点对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="30%"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="appKey" prop="appKey">
              <el-input v-model="form.appKey" placeholder="请输入appKey" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="密钥" prop="secret">
              <el-input v-model="form.secret" placeholder="请输入密钥" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="公司名称" prop="companyName">
              <el-input
                v-model="form.companyName"
                placeholder="请输入公司名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
           <el-row>
            <el-col :span="24">
                <el-form-item label="选择工点" prop="permissions">
                    <el-tree
                        :data="treeListData"
                        show-checkbox
                        ref="tree"
                        node-key="deptId"
                        :props="defaultProps"
                    />
                </el-form-item>
            </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :loading="submitLoading"
          >确 定</el-button
        >
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Vue from 'vue'
import { getDeptTreeDeptData } from '@/services/authorityManagement/managePersonnel'
import { getDataList, addData, updateData, delData } from '@/services/system/appKey'

export default Vue.extend({
  data () {
    return {
      // 遮罩层
      loading: true,
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        // pageNum: 1,
        // pageSize: 10,
        appKey: undefined,
        companyName: undefined
      },
      defaultProps: {
        children: 'children',
        label: 'deptName'
      },
      // 表单校验
      rules: {},
      submitLoading: false,
      postList: [],
      treeListData: [],
      open: false,
      title: ''
    }
  },
  watch: {
    // 根据名称筛选部门树
    deptName (val) {
      this.$refs.deptTreeRef.filter(val)
    }
  },
  created () {
    this.getList()
    this.treeList()
  },
  methods: {
    // 筛选节点
    filterNode (value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    recursionFun (data, arr) {
      if (!data[0].children) {
        this.defaultCheckedKeys = arr
        return this.$refs.deptTreeRef.setCurrentKey(data[0].id)
      } else {
        arr.push(data[0].id)
        this.recursionFun(data[0].children, arr)
      }
    },
    /** 查询岗位列表 */
    getList () {
      this.loading = true
      getDataList(this.queryParams).then((response) => {
        this.postList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 授权点取消按钮
    authorizationPointCancel () {
      this.open = false
      this.reset()
      this.resetData()
    },
    // 表单重置
    reset () {
      this.form = {}
      this.resetForm('form')
      this.$nextTick(() => {
        this.$refs.tree && this.$refs.tree.setCheckedKeys([])
      })
    },
    // 搜索按钮操作
    handleQuery () {
      this.getList()
    },
    // 重置按钮操作
    resetQuery () {
      this.resetForm('queryForm')
      this.cascaderValue = []
      this.handleQuery()
    },
    // 新增按钮操作
    handleAdd () {
      this.reset()
      this.open = true
      this.title = '新增'
    },
    treeList () {
      getDeptTreeDeptData().then((res) => {
        this.treeListData = res.data
      })
    },
    // 打开修改弹窗
    handleEdit (row) {
      // 设置tree回显
      this.form = { ...row }
      this.open = true
      const keys = this.form.permissions.map(item => item.orgId)
      console.log(1111111111111, keys, this.$refs.tree)
      this.$nextTick(() => {
        this.$refs.tree && this.$refs.tree.setCheckedKeys(keys)
      })
    },
    // 提交按钮
    async submitForm () {
      // 判断是否新增
      let res = {}
      let permissions = this.$refs.tree.getCheckedKeys()
      permissions = permissions.map(id => ({ orgId: id }))
      const api = this.form.id ? updateData : addData
      if (this.form.id) {
        delete this.form.createdAt
        delete this.form.modifiedAt
        delete this.form.permissionStr
      }

      res = await api({ ...this.form, permissions })
      // 接口错误处理
      if (res.code !== 200) return this.$message.error('后端接口异常')

      // 成功后操作
      this.getList()
      this.$message.success(res.msg)
      this.open = !this.open
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      this.$confirm('是否确认删除?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function () {
          return delData(row.id)
        })
        .then(() => {
          this.getList()
          this.$message.success('删除成功')
        })
    }
  }
})
</script>
<style scoped>
</style>
